
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for view_blog_comments.
	''' </summary>
	Public Partial Class view_blog_comments
		Inherits BaseWebPage
		Protected trUserMenu As System.Web.UI.HtmlControls.HtmlTableRow
		Protected trGroupMenu As System.Web.UI.HtmlControls.HtmlTableRow
		Protected hlGroupBulletin As System.Web.UI.WebControls.HyperLink
		Protected hlGroupTrash As System.Web.UI.WebControls.HyperLink
		Protected hlGroupBlog As System.Web.UI.WebControls.HyperLink
		Protected hlPostNewBlog As System.Web.UI.WebControls.HyperLink
		'protected System.Web.UI.WebControls.HyperLink	hlReturnToBlogList;

		Protected hlUMInbox As System.Web.UI.WebControls.HyperLink
		Protected hlUMSent As System.Web.UI.WebControls.HyperLink
		Protected hlUMGroupBulletin As System.Web.UI.WebControls.HyperLink
		Protected lblUMBlog As System.Web.UI.WebControls.Label
		Protected hlUMTrash As System.Web.UI.WebControls.HyperLink
		Protected lblGMGroupBlog As System.Web.UI.WebControls.Label

		Private blogID As Integer = -1
		Protected grpID As Integer = -1
		Protected grp As Group = Nothing
		Protected usr As User = Nothing
		Protected isGroupMode As Boolean = False
		Protected isModerator As Boolean = False

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Auto login from an external link
			'AutoLogin( Request );

			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.VIEW_BLOG_COMMENTS_PAGE;

			' init user
			usr = LoggedUser

			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			' init page from XML
			Dim htPageConstants As New Hashtable()

			Me.initPageTitles(Constants.VIEW_BLOG_COMMENTS, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)
			' init tabs
			ucBlogTabs.ActiveTab = 1
			ucBlogTabs.PageName = Constants.VIEW_BLOG_COMMENTS
			grpBlogTabs.ActiveTab = 2
			grpBlogTabs.PageName = Constants.VIEW_BLOG_COMMENTS

			Me.RegisterStartupScript("resizeImages", Constants.RESIZE_BLOG_IMAGE_SCRIPT)
			Me.pageTemplate.onLoadScript = "resizeImages()"

			' init blog
			If Request.Params("id") = Nothing OrElse Request.Params("id") = "" Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			Try
				blogID = Convert.ToInt32(Request.Params("id"))
			Catch
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End Try

			' init group

			If Request.Params(Constants.GROUP_PARAM) <> Nothing AndAlso Request.Params(Constants.GROUP_PARAM) <> "" Then
				isGroupMode = True

				Try
					grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
				Catch
					Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
				End Try

				grp = Groups.GetGroupByID(grpID)
				If grp = Nothing Then
					Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
				End If

				isModerator = (grp.Uid = usr.ID)
			End If

			If isGroupMode Then
				' init menu
				ucBlogTabs.Visible = False
				grpBlogTabs.Visible = True

				' init links
				'hlPostNewBlog.NavigateUrl = "../" + Constants.SEND_BLOG + "?" + Constants.GROUP_PARAM + "=" + grp.ID;
				'hlPostNewBlog.Text = GetPageConstText(htPageConstants, "CONST_PostNewGroupBlog_TEXT"); //"Post new group blog";
				'hlReturnToBlogList.NavigateUrl = "../" + Constants.BLOG + "?" + Constants.GROUP_PARAM + "=" + grp.ID;
				'hlReturnToBlogList.Text =  GetPageConstText(htPageConstants, "CONST_ReturnToGroupBlogList_TEXT"); //"Return to group blog list";
				hlBackToGroup.NavigateUrl = "../" + Constants.GROUP_WELCOME + "?" + Constants.GROUP_PARAM + "=" + grpID
				hlBackToGroup.Text = GetPageConstText(htPageConstants, "CONST_GroupBack_TEXT") + grp.Name
				'Constants.GROUP_BACK_LINK
				' init page title
				litMainPageTitle.Text = GetPageConstText(htPageConstants, "CONST_GroupMainPageTitle_TEXT")
				'"Group Blog";
				' init group blog comments 
				Dim grpBlog As GroupBlog = Messages.GetGroupBlog(grp.ID, blogID)
				Dim grpBlogComments As ArrayList = Messages.GetGroupBlogEntries(blogID)
				Dim grpNewBlogComments As ArrayList = Messages.GetNotApprovedGroupBlogEntries(grp.ID, blogID)
				Dim nGroupBlogCommentsInTotal As Integer = grpNewBlogComments.Count + grpBlogComments.Count

				If Not isModerator Then
					hlEdit.Visible = False
					lbRemove.Visible = False
					'hlPostNewBlog.Visible = false;
					lblEditDash.Visible = False
					lblEditDash2.Visible = False
					nGroupBlogCommentsInTotal = grpBlogComments.Count
				End If

				' Init group blog.

				Dim curBlogDate As DateTime = grpBlog.DateSent
				lblDate.Text = curBlogDate.DayOfWeek + ", " + Constants.ARR_MONTHS(curBlogDate.Month - 1) + " " + curBlogDate.Day + ", " + curBlogDate.Year
				lblSubject.Text = Server.HtmlEncode(grpBlog.Subject)
				litBlogBody.Text = Me.initBlogMessage(grpBlog.Body)
				lblTimePosted.Text = curBlogDate.ToShortTimeString()
				lblCommentsCount.Text = nGroupBlogCommentsInTotal.ToString() + " " + "Comment"
				If nGroupBlogCommentsInTotal <> 1 Then
					lblCommentsCount.Text += "s"
				End If

				hlEdit.NavigateUrl = "../" + Constants.SEND_BLOG + "?id=" + blogID + "&" + Constants.GROUP_PARAM + "=" + grp.ID
			Else
				' init menu
				ucBlogTabs.Visible = True
				grpBlogTabs.Visible = False
				'hlReturnToBlogList.NavigateUrl = "../" + Constants.BLOG;

				Dim blog As Blog = Messages.GetUserBlog(usr.ID, blogID)
				Dim blogComments As ArrayList = Messages.GetUserBlogEntries(blogID)
				Dim blogNewComments As ArrayList = Messages.GetNotApprovedBlogEntries(blogID)

				Dim nBlogCommentsInTotal As Integer = blogComments.Count + blogNewComments.Count

				Dim curBlogDate As DateTime = blog.DateSent
				lblDate.Text = curBlogDate.DayOfWeek + ", " + Constants.ARR_MONTHS(curBlogDate.Month - 1) + " " + curBlogDate.Day + ", " + curBlogDate.Year
				lblSubject.Text = Server.HtmlEncode(blog.Subject)
				litBlogBody.Text = Me.initBlogMessage(blog.Body)
				lblTimePosted.Text = curBlogDate.ToShortTimeString()
				lblCommentsCount.Text = nBlogCommentsInTotal.ToString() + " " + "Comment"
				If nBlogCommentsInTotal <> 1 Then
					lblCommentsCount.Text += "s"
				End If

				hlEdit.NavigateUrl = "../" + Constants.SEND_BLOG + "?id=" + blogID
			End If

			If Not IsPostBack Then
				BindGrid()
			End If
		End Sub
		Protected Function CreateDataSource() As ICollection
			Dim dt As New DataTable()
			Dim curBlogDate As DateTime
			Dim blogEntries As ArrayList = Nothing
			Dim grpNewBlogEntries As ArrayList = Nothing
			Dim LoggedUsersList As ArrayList = LoggedUsersList = Common.Utils.GetLoggedUsersIDs()

			If isGroupMode Then
				' List group blog comments
				blogEntries = Messages.GetGroupBlogEntries(blogID)
				If isModerator Then
					grpNewBlogEntries = Messages.GetNotApprovedGroupBlogEntries(grp.ID, blogID)
					Dim tmp As New ArrayList()
					tmp = blogEntries
					blogEntries = grpNewBlogEntries
					blogEntries.AddRange(tmp)
				End If
			Else
				' List user blog comments
				blogEntries = Messages.GetNotApprovedBlogEntries(blogID)
				blogEntries.AddRange(Messages.GetUserBlogEntries(blogID))
			End If

			If blogEntries.Count = 0 Then
				lblNoBlog.Visible = True
			Else
				lblNoBlog.Visible = False
			End If
			Dim dr As DataRow
			dt.Columns.Add(New DataColumn("SenderID", GetType(Integer)))
			dt.Columns.Add(New DataColumn("SenderName", GetType(String)))
			dt.Columns.Add(New DataColumn("SenderImageURL", GetType(String)))
			dt.Columns.Add(New DataColumn("OnlineImg", GetType(String)))
			dt.Columns.Add(New DataColumn("MemberURL", GetType(String)))
			dt.Columns.Add(New DataColumn("Date", GetType(String)))
			dt.Columns.Add(New DataColumn("Time", GetType(String)))
			dt.Columns.Add(New DataColumn("Body", GetType(String)))
			dt.Columns.Add(New DataColumn("BlogID", GetType(String)))
			dt.Columns.Add(New DataColumn("RemoveVisibility", GetType(Boolean)))
			dt.Columns.Add(New DataColumn("ApproveVisibility", GetType(Boolean)))
			dt.Columns.Add(New DataColumn("OnClickJS", GetType(String)))
			dt.Columns.Add(New DataColumn("SenderNameFull", GetType(String)))
			'dt.Columns.Add(new DataColumn("Subject", typeof(string)));

			Dim temp As UserPicture
			If isGroupMode Then
				' Init group blog comments

				' List group's blog comments
				For Each blog As GroupBlog In blogEntries
					dr = dt.NewRow()
					curBlogDate = blog.DateSent
					dr(0) = blog.SenderID
					dr(1) = getBeginning(HttpUtility.HtmlEncode(blog.Sender.Name), Constants.MAX_NAME_LENGTH_ABOVE_IMAGE)
					temp = blog.Sender.DefaultPicture
					If temp <> Nothing Then
						dr(2) = Pictures.GetPictureSrcFullURL(temp, True, "../")
					Else
						dr(2) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
					End If

					If LoggedUsersList.Contains(blog.SenderID) Then
						dr(3) = "../images/online_status.gif"
					Else
						dr(3) = "../images/spacer.gif"
					End If

					dr(4) = Users.ComposeMemeberLink(blog.Sender)
					dr(11) = Users.ComposeMemeberLinkOnClickJS(blog.Sender, trUserCanceledMessage)

					dr(5) = curBlogDate.DayOfWeek + ", " + Constants.ARR_MONTHS(curBlogDate.Month - 1) + " " + curBlogDate.Day + ", " + curBlogDate.Year
					dr(6) = curBlogDate.ToShortTimeString()

					dr(7) = Me.initBlogMessage(blog.Body)
					dr(8) = blog.ID
					dr(9) = isModerator
					dr(10) = (blog.Status = GroupBlog.BlogStatus.gbsInitial)
					dr(12) = HttpUtility.HtmlEncode(blog.Sender.Name)
					'dr[10] = blog.Subject;
					dt.Rows.Add(dr)

				Next
			Else
				' List user's blog comments
				For Each blog As Blog In blogEntries
					dr = dt.NewRow()
					curBlogDate = blog.DateSent
					dr(0) = blog.SenderID
					dr(1) = getBeginning(HttpUtility.HtmlEncode(blog.Sender.Name), Constants.MAX_NAME_LENGTH_ABOVE_IMAGE)
					temp = blog.Sender.DefaultPicture
					If temp <> Nothing Then
						dr(2) = Pictures.GetPictureSrcFullURL(temp, True, "../")
					Else
						dr(2) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
					End If

					If LoggedUsersList.Contains(blog.SenderID) Then
						dr(3) = "../images/online_status.gif"
					Else
						dr(3) = "../images/spacer.gif"
					End If

					dr(4) = Users.ComposeMemeberLink(blog.Sender)
					dr(11) = Users.ComposeMemeberLinkOnClickJS(blog.Sender, trUserCanceledMessage)

					dr(5) = curBlogDate.DayOfWeek + ", " + Constants.ARR_MONTHS(curBlogDate.Month - 1) + " " + curBlogDate.Day + ", " + curBlogDate.Year
					dr(6) = curBlogDate.ToShortTimeString()

					dr(7) = Me.initBlogMessage(blog.Body)
					dr(8) = blog.ID
					dr(9) = True
					dr(10) = (blog.Status = BaseBlog.BlogStatus.gbsInitial)
					dr(12) = HttpUtility.HtmlEncode(blog.Sender.Name)
					'dr[10] = blog.Subject;
					dt.Rows.Add(dr)
				Next
			End If
			Dim dv As New DataView(dt)
			Return dv
		End Function
		Protected Sub BindGrid()
			dgMessages.DataSource = CreateDataSource()
			dgMessages.DataBind()
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dgMessages.ItemCommand, AddressOf Me.dgMessages_ItemCommand

		End Sub
#End Region

		Protected Sub lbAddComment_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			If isGroupMode Then
				Response.Redirect(Constants.POST_BLOG_COMMENT + "?id=" + blogID + "&" + Constants.GROUP_PARAM + "=" + grp.ID)
			Else
				Response.Redirect(Constants.POST_BLOG_COMMENT + "?id=" + blogID)
			End If
		End Sub

		Private Sub dgMessages_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
			Dim btnPushed As LinkButton = DirectCast(e.CommandSource, LinkButton)
			Dim cmd As String = e.CommandName
			Select Case cmd
				Case "Remove"
					If isGroupMode Then
						' Remove blog comment
						Dim grpBlog As GroupBlog = Messages.GetGroupBlogEntry(grp.ID, Convert.ToInt32(e.CommandArgument))
						grpBlog.Delete()
						Response.Redirect(Constants.VIEW_BLOG_COMMENTS + "?id=" + blogID + "&" + Constants.GROUP_PARAM + "=" + grp.ID)
					Else
						Dim blog As Blog = Messages.GetBlogEntry(Convert.ToInt32(e.CommandArgument))
						blog.Delete()
						Response.Redirect(Constants.VIEW_BLOG_COMMENTS + "?id=" + blogID)
					End If
					Exit Select
				Case "Approve"
					If isGroupMode Then
						' Accept blog comment
						Dim grpBlog As GroupBlog = Messages.GetGroupBlogEntry(grp.ID, Convert.ToInt32(e.CommandArgument))
						grpBlog.Approve()
						grpBlog.Save()
						Response.Redirect(Constants.VIEW_BLOG_COMMENTS + "?id=" + blogID + "&" + Constants.GROUP_PARAM + "=" + grp.ID)
					Else
						Dim blog As Blog = Messages.GetBlogEntry(Convert.ToInt32(e.CommandArgument))
						blog.Approve()
						blog.Save()
						Response.Redirect(Constants.VIEW_BLOG_COMMENTS + "?id=" + blogID)
					End If
					Exit Select
			End Select
		End Sub

		Protected Sub lbRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			If isGroupMode Then
				' Remove blog
				Dim blog As GroupBlog = Messages.GetGroupBlogEntry(grp.ID, Convert.ToInt32(blogID))
				blog.Delete()
				Response.Redirect(Constants.BLOG + "?" + Constants.GROUP_PARAM + "=" + grp.ID)
			Else
				Dim blog As Blog = Messages.GetUserBlogEntry(LoggedUser.ID, Convert.ToInt32(blogID))
				blog.Delete()
				Response.Redirect(Constants.BLOG)
			End If
		End Sub

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
		Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_Form1
			End Get
		End Property
		'set { Migrated_Form1 = value; }
	End Class
End Namespace






